// const { app, BrowserWindow } = require("electron");
// const path = require("path");

import { app, BrowserWindow, Menu } from "electron";
// 最新 node 核心包的导入写法
import { fileURLToPath } from "node:url";
import path, { dirname } from "node:path";
// // 获取 __filename 的 ESM 写法
// const __filename = fileURLToPath(import.meta.url)
// 获取 __dirname 的 ESM 写法
const __dirname = dirname(fileURLToPath(import.meta.url));

// 应用名称
const APP_NAME = "融合通信";
// 应用图标路径
const APP_ICON = path.join(__dirname, "public", "app-icon.png");

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    icon: APP_ICON,
    frame: true,
    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
      nodeIntegration: true,
      contextIsolation: false,
    },
  });
  Menu.setApplicationMenu(null); // 去掉菜单

  const isDev = false;
  if (isDev) {
    win.loadURL("http://localhost:5173");
  } else {
    win.loadFile("dist/index.html");
  }
}

app.setName(APP_NAME);

app.whenReady().then(() => {
  createWindow();

  app.on("activate", function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on("window-all-closed", function () {
  if (process.platform !== "darwin") app.quit();
});
